package io.framework.modules.water.controller;

import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.utils.Result;
import io.framework.modules.station.entity.StationEntity;
import io.framework.modules.station.service.StationService;
import io.framework.modules.water.dto.OceanBuoyPointDTO;
import io.framework.modules.water.service.OceanBuoyPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 海洋浮标点信息
 *
 * @author Gujq email
 * @since 1.0.0 2020-09-22
 */
@RestController
@RequestMapping("obp/obp")
@Api(tags = "海洋浮标点信息")
public class OceanBuoyPointController {

    @Autowired
    private OceanBuoyPointService oceanBuoyPointService;

    @Autowired
    private StationService stationService;

    @GetMapping("pageList")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "time", value = "监测月份（例：2020-01）", paramType = "query", dataType = "String"),
    })
//    @RequiresPermissions("noise:noisemonitor:page")
    public Result<PageData<OceanBuoyPointDTO>> pageList(@ApiIgnore @RequestParam Map<String, Object> params) {
        if (!params.containsKey(Constant.PAGE) || !params.containsKey(Constant.LIMIT)) {
            return new Result<PageData<OceanBuoyPointDTO>>().error("参数错误");
        }
        PageData<OceanBuoyPointDTO> page = oceanBuoyPointService.pageList(params);
        if (page.getTotal() > 0) {
            List<OceanBuoyPointDTO> list = page.getList();
            List<String> codes = list.stream().map(oceanBuoyPointEntity -> oceanBuoyPointEntity.getStationCode()).collect(Collectors.toList());
            List<StationEntity> stationEntities = stationService.getListByCodes(codes);
            list.stream().forEach(oceanBuoyPointEntity -> {
                StationEntity stationEntity = stationEntities.stream().filter(s -> s.getCode().equals(oceanBuoyPointEntity.getStationCode())).findFirst().get();
                if (stationEntity != null) oceanBuoyPointEntity.setStationName(stationEntity.getName());
            });
        }
        return new Result<PageData<OceanBuoyPointDTO>>().ok(page);
    }

}
